package com.axend.aerosense.base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

import com.axend.aerosense.base.BaseModuleInit;
import com.axend.aerosense.base.app.BaseApplication;

import java.util.List;

/**
 * wifi工具类
 */
public class WifiUtils {

    private WifiManager mWifiManager;

    public static WifiUtils getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder{
        private static final WifiUtils INSTANCE = new WifiUtils();
    }

    private WifiUtils(){
        mWifiManager = (WifiManager) BaseApplication.getInstance().getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
    }

    public WifiManager getWifiManager() {
        return mWifiManager;
    }

    /** 断开wifi链接 */
    public void disconnect(){
        mWifiManager.disconnect();
    }

    /**
     * 获取当前连接的wifi名
     *
     * @return
     */
    public String getWifiSSID() {
        WifiInfo info = mWifiManager.getConnectionInfo();
        String wifiId = info != null ? info.getSSID() : null;
        if (wifiId != null){
            wifiId = wifiId.substring(1, wifiId.length() - 1);
        }
        return wifiId;
    }

    /**
     * 是否开启wifi，没有的话打开wifi
     * @return Android 10 ：返回wifi的是否打开，小于Android 10返回wifi是否打开成功
     */
    public boolean openWifi() {
        boolean isOpen = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            isOpen = mWifiManager.isWifiEnabled();
        }else {
            if (!mWifiManager.isWifiEnabled()) {
                isOpen = mWifiManager.setWifiEnabled(true);
            }
        }
        return isOpen;
    }

    public boolean isEnable(){
        return mWifiManager.isWifiEnabled();
    }

    /**
     * 判断wifi频段是否为5G
     * @param freq
     * @return
     */
    public boolean is5GHz(int freq) {
        return freq > 4900 && freq < 5900;
    }

    /**
     * 关闭wifi
     */
    public void closeWifi() {
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
        }
    }

    /** 开始扫描wifi */
    public void startScan(){
        if (null == mWifiManager) { return; }
        mWifiManager.startScan();
    }

    /** 获取wifi扫描结果 */
    public List<ScanResult> getScanResults(){
        if (null == mWifiManager) { return null; }
        return mWifiManager.getScanResults();
    }

    /**
     * 连接WiFi
     *
     * @param wifiName
     * @param password
     * @param type
     */
    @SuppressLint({"WifiManagerLeak", "MissingPermission"})
    public void connectWifi(String wifiName, String password, String type) {
        // 1、注意热点和密码均包含引号，此处需要需要转义引号
        String ssid = "\"" + wifiName + "\"";
        String psd = "\"" + password + "\"";

        //2、配置wifi信息
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = ssid;
        switch (type) {
            case "WEP":
                // 加密类型为WEP
                conf.wepKeys[0] = psd;
                conf.wepTxKeyIndex = 0;
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                break;
            case "WPA":
                // 加密类型为WPA
                conf.preSharedKey = psd;
                break;
            default:
                //无密码
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }
        //3、链接wifi
        mWifiManager.addNetwork(conf);
        //此处重复添加，跳过系统验证wifi机制
        mWifiManager.addNetwork(conf);
//        mWifiManager.addNetworkSuggestions();
        List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals(ssid)) {
                mWifiManager.disconnect();
                mWifiManager.enableNetwork(i.networkId, true);
                mWifiManager.reconnect();
                break;
            }
        }
    }
}
